{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyOBPrcU1tfIZBseJjPTt54o"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Fine-Tuning Dataset Builder - Parametric Queries"
      ],
      "metadata": {
        "id": "wTs3a7OkdRtO"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "In order to run this notebook you will need a non-empty graph database in Neo4j together with its credentials.\n",
        "\n",
        "**In this notebook:**\n",
        "\n",
        "- extract schema information from the graph,\n",
        "- extract sample data from the graph (nodes and relationships instances),\n",
        "- process the schema and the extracted data from the graph,\n",
        "- generate sample queries using predefined Python functionalities,\n",
        "- save the generated data to files.\n"
      ],
      "metadata": {
        "id": "SB3oKYODpwxu"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Workspace Setup"
      ],
      "metadata": {
        "id": "BGqjVB2YytME"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "tvScvlRDyrtC"
      },
      "outputs": [],
      "source": [
        "%pip install neo4j"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Load and mount the drive helper (needed for Google Colab only)\n",
        "from google.colab import drive\n",
        "\n",
        "# This will prompt for authorization\n",
        "drive.mount('/content/drive')\n",
        "\n",
        "# Set the working directory\n",
        "%cd '/content/drive/MyDrive/finetuneCypher/'"
      ],
      "metadata": {
        "id": "WSGifOcWy6C0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Upload the Neo4j graph database credentials (from Colab secrets)\n",
        "from google.colab import userdata\n",
        "URI = userdata.get('URI')\n",
        "PWD = userdata.get('PWD')"
      ],
      "metadata": {
        "id": "RxRtMw8qlu9c"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Import the local modules\n",
        "\n",
        "# Python functionalities to collect and save data samples\n",
        "from utils.utilities import *\n",
        "# Neo4j graph connector\n",
        "from utils.neo4j_conn import *\n",
        "# Functionalities to extract schema and data from the graph\n",
        "from utils.neo4j_schema import *\n",
        "# Functionalities to parse extracted graph data\n",
        "from utils.graph_utils import *"
      ],
      "metadata": {
        "id": "zIX3pjHH1GHq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Initialize the Neo4j connector module\n",
        "graph = Neo4jGraph(url=URI, username='neo4j', password=PWD, database='neo4j')\n",
        "\n",
        "# Module to extract data from the graph\n",
        "gutils = Neo4jSchema(url=URI, username='neo4j', password=PWD, database='neo4j')"
      ],
      "metadata": {
        "id": "wYKtthtXkmlB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# How to query the graph using the 'graph' module\n",
        "graph.query(\"MATCH (n) RETURN COUNT(n) AS TotalNodes\")"
      ],
      "metadata": {
        "id": "DhQ1mHSYJ-xj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Path Variables"
      ],
      "metadata": {
        "id": "hS7uJ7gBQsQc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a path variable for the data folder\n",
        "data_path = '/content/drive/MyDrive/finetuneCypher/datas/'\n",
        "\n",
        "# Graph schema file\n",
        "schema_file = 'schema_file.json'\n",
        "\n",
        "# Node and relationships instances\n",
        "node_instances_file = 'node_instances_file.json'\n",
        "rels_instances_file = 'rels_instances_file.json'\n",
        "\n",
        "# Fine-tuning datasets\n",
        "trainer_with_repeats_file = 'parametric_trainer_with_repeats.json'\n",
        "trainer_without_repeats_file = 'parametric_trainer_without_repeats.json'"
      ],
      "metadata": {
        "id": "P2-qo45sxUii"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Options For Building the Supervised Fine Tuning Dataset"
      ],
      "metadata": {
        "id": "5WOC4N1DoZdB"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "- The node and relationships instances extracted from the graph illustrate realistic property values. The number of instances extracted depends on the graph's size and the complexity of the data.\n",
        "- `ALLOW_REPEATS` denotes the method by which SFT messages are constructed from the extracted instances. An example of repeated queries (where only the value of the node's property varies) is shown below:\n",
        "```\n",
        "MATCH (n:Article {author: 'John Smith'}) RETURN n\n",
        "MATCH (n:Article {author: 'Jane Doe'}) RETURN n\n",
        "```\n",
        "- In larger graphs, an individual sampler (which may include variations of the same query type, with or without repeats) can generate tens or hundred of thousands variations. The `M` parameter limits these values to ensure a more balanced dataset. If desired, each upper limit can be individually adjusted within the functions below.\n"
      ],
      "metadata": {
        "id": "YzVl30pFKloa"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Choose how many instances of each node label to extract\n",
        "node_instances_size = 12\n",
        "\n",
        "# Choose how many instances of each relationship type to extract\n",
        "rels_instances_size = 12\n",
        "\n",
        "# Choose if to include repeats in the data builder or not\n",
        "ALLOW_REPEATS = True\n",
        "\n",
        "# Select the maximum size of each individual sampler with len(sampler) elements\n",
        "M = 500"
      ],
      "metadata": {
        "id": "KWyupXnLNIjV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Data Collection for Sample Building\n",
        "\n",
        "**NOTES:**\n",
        "\n",
        "This section necessitates access to a Neo4j database populated with nodes and relationships. Data relevant to the task is retrieved from the knowledge graph, processed, and then stored in files.\n",
        "\n"
      ],
      "metadata": {
        "id": "5VbHtpzUv30h"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Build the schema as a dictionary\n",
        "jschema = gutils.get_structured_schema\n",
        "# Check the output format\n",
        "jschema.keys()"
      ],
      "metadata": {
        "id": "RaHAu-aMv8db"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract the list of nodes\n",
        "nodes = get_nodes_list(jschema)\n",
        "# Extract the list of relationships\n",
        "relationships = jschema['relationships']"
      ],
      "metadata": {
        "id": "zNsAfIypkn7K"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract the node instances from the graph\n",
        "node_instances = gutils.extract_node_instances(nodes, node_instances_size)\n",
        "\n",
        "# Extract the relationship instances from the graph\n",
        "rels_instances = gutils.extract_multiple_relationships_instances(relationships, rels_instances_size)\n",
        "\n",
        "# Serialize extracted neo4j.time data - for saving to json files\n",
        "nodes_instances_serialized = serialize_nodes_data(node_instances)\n",
        "rels_instances_serialized = serialize_relationships_data(rels_instances)"
      ],
      "metadata": {
        "id": "5wwJZ6dRwK9w"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Save data to json files\n",
        "write_json(jschema, data_path+schema_file)\n",
        "write_json(nodes_instances_serialized, data_path+node_instances_file)\n",
        "write_json(rels_instances_serialized, data_path+rels_instances_file)"
      ],
      "metadata": {
        "id": "4yv5NR6wwZDI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Data Preparation for Sample Building\n",
        "\n"
      ],
      "metadata": {
        "id": "qU_sGpI8vrKH"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "From this point forward, connection to the Neo4j knowledge graph is not required, as the necessary data has been saved to JSON files.\n",
        "\n",
        "Some queries constructed below require the use of specific data type formats. For instance, the query for \"Find all the titles that contain 'approximation'!\" is best suitted for string data type properties. To select samples that meet specific conditions, we build several dictionaries that organize the data according to the property's data type."
      ],
      "metadata": {
        "id": "51fYvsaMo24T"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Read the data from files if previously saved\n",
        "jschema = read_json(data_path+schema_file)\n",
        "node_instances = read_json(data_path+node_instances_file) # these are serialized, see above\n",
        "rels_instances = read_json(data_path+rels_instances_file) # these are serialized, see above"
      ],
      "metadata": {
        "id": "4oIeNJWPvy5c"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# List of node labels\n",
        "nodes = get_nodes_list(jschema)\n",
        "\n",
        "# Read the nodes with their properties and datatypes\n",
        "node_props_types = jschema['node_props']\n",
        "\n",
        "# Read the relationship properties with their datatypes\n",
        "rel_props_types = jschema['rel_props']\n",
        "\n",
        "# Read the relationships as a list of triples\n",
        "relationships = jschema['relationships']"
      ],
      "metadata": {
        "id": "6jJs939cwxVU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# List of datatypes available as node properties in the graph\n",
        "node_dtypes = retrieve_datatypes(jschema, \"node\")\n",
        "print(f\"The datatypes for node properties: {node_dtypes}\")"
      ],
      "metadata": {
        "id": "M7P8mQGT1Uck"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# List of datatypes available as relationship properties in the graph\n",
        "rel_dtypes = retrieve_datatypes(jschema, \"rel\")\n",
        "print(f\"The datatypes for relationships properties: {rel_dtypes}\")"
      ],
      "metadata": {
        "id": "_rO1_qIWibFI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "- In the next cell, a dictionary is created with keys named `datatype_parsed`, where `datatype` corresponds to one of the potential data types of node properties as outlined in `node_dtypes` and exemplified in the extracted node instances.\n",
        "\n",
        "- Additionally, a consolidated list of all data types is included under the key `dtypes_parsed`.\n",
        "\n",
        "- The dictionary values are lists of triples in the format `[label, property, value]`."
      ],
      "metadata": {
        "id": "2L4ewz1rPVYW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract and parse n instances of specified datatype, return a flatten list\n",
        "dparsed = {f\"{datatype.lower()}_parsed\": \\\n",
        "                        parse_node_instances_datatype(jschema,\n",
        "                                                      node_instances,\n",
        "                                                      nodes,\n",
        "                                                      datatype,\n",
        "                                                      True) for datatype in node_dtypes\n",
        "                              }\n",
        "# Add all the combined records\n",
        "dparsed['dtypes_parsed'] = sum(dparsed.values(), [])\n",
        "\n",
        "# Display available lists of instances\n",
        "print(f\"A dictionary is created, the keys are: {dparsed.keys()}.\")\n",
        "\n",
        "# Display a sample entry - instance of node and property with datatype STRING\n",
        "print(f\"Sample entry: {dparsed['string_parsed'][11]}.\")\n"
      ],
      "metadata": {
        "id": "onMSAPLh0pmR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "Building on the concepts from the previous step, we select relationships where the start and end nodes have properties of a specified data type. This results in the creation of a dictionary with keys formatted as `datatypeStart_datatypeEnd_rels`. The `all_rels` values contain all the possible combinations of data types."
      ],
      "metadata": {
        "id": "08rjhk1nS2mT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Generate all possible pairs of node properties datatypes\n",
        "dtypes_pairs = list(product(node_dtypes, repeat=2))\n",
        "\n",
        "# Use dictionary comprehension with formatted keys for pairs\n",
        "drels = {\n",
        "    f\"{dt1.lower()}_{dt2.lower()}_rels\": \\\n",
        "    filter_relationships_instances(jschema, rels_instances, dt1, dt2)\n",
        "    for dt1, dt2 in dtypes_pairs\n",
        "}\n",
        "\n",
        "# Add 'all_rels' key with concatenated lists from the other values\n",
        "drels['all_rels'] = sum(drels.values(), [])\n",
        "\n",
        "# Retain all those combinations that have nonempty entries\n",
        "drels = {key: value for key, value in drels.items() if value}\n",
        "\n",
        "# Display the list of node properties datatypes combinations for the relationships in the graph\n",
        "print(f\"The possible end node properties datatypes pairs for relationships are\\n {drels.keys()}.\\n\")\n",
        "\n",
        "# Sample entry\n",
        "print(\"A sample entry:\")\n",
        "drels['string_string_rels'][1]"
      ],
      "metadata": {
        "id": "KVqnDO0R4fc0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "- We construct a dictionary that considers the data types of properties associated with relationships.\n",
        "- The keys are formatted as: `datatypeStart_datatypeRelationship_datatypeEnd_rels`. The `all_rels` values contain all the possible combinations of data types."
      ],
      "metadata": {
        "id": "lKWIrwxmT_SO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Retrieve those instances for which relations have properties attached\n",
        "instances_with_rel_props = retrieve_instances_with_relationships_props(rels_instances)\n",
        "print(f\"There are {len(instances_with_rel_props)} relationship(s) with properties in the graph.\")\n",
        "\n",
        "# Display a sample\n",
        "instances_with_rel_props[0][0]"
      ],
      "metadata": {
        "id": "21xsHs_VnwYz"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Use dictionary comprehension with formatted keys for pairs\n",
        "drelsprops = {\n",
        "    f\"{dt1.lower()}_{rt.lower()}_{dt2.lower()}_rels\": \\\n",
        "    filter_relationships_with_props_instances(jschema, instances_with_rel_props, dt1, rt, dt2)\n",
        "    for dt1, dt2 in dtypes_pairs\n",
        "    for rt in rel_dtypes\n",
        "    if filter_relationships_with_props_instances(jschema, instances_with_rel_props, dt1, rt, dt2)\n",
        "}\n",
        "\n",
        "# Add 'all_rels' key with concatenated lists from the other values\n",
        "drelsprops['all_rels'] = sum(drelsprops.values(), [])\n",
        "\n",
        "# Available combinations for source-relationship-target property datatypes\n",
        "print(f\"The available combinations are {list(drelsprops.keys())}.\")\n",
        "\n",
        "# Sample entry\n",
        "drelsprops['integer_string_string_rels'][6]"
      ],
      "metadata": {
        "id": "1heeSK5YuM3r"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Samples Builder"
      ],
      "metadata": {
        "id": "Uwuis4Fo9bEB"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "- Each cell below features a function that constructs a message with four components: a system prompt, a question, subschema (relevant information about the graph), and a parametric Cypher query.\n",
        "\n",
        "- Notation details:\n",
        "    - Node labels: `label_i`\n",
        "    - Node properties: `prop_i`\n",
        "    - Node properties values: `val_i`\n",
        "    - Relationship types: `rtype_i`\n",
        "    - Relationship properties: `rprop_i`\n",
        "    - Relationship properties values: `rval_i`\n",
        "\n",
        "- The subschema information is derived using:\n",
        "```\n",
        "graph_utils.build_minimal_subschema(jschema,\n",
        "    nodes_info, relationships_info,\n",
        "    include_node_props, include_rel_props, include_types)\n",
        "```\n",
        "- `nodes_info` is formatted as `[[label_1, prop_1], ...]`\n",
        "- `relationship_info` follows the format `[[rtype_1, rprop_1],...]`.\n",
        "- The last three parameters are boolean values that dictate the extent of the information included in subschema.\n",
        "\n",
        "- To exclude any of the generated groups of samples, in the corresponding cell, comment the line:  \n",
        "```trainer += collect_samples(sampler, M)```"
      ],
      "metadata": {
        "id": "sVsiLJQjoR3b"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a system message\n",
        "system_message =  \"Convert the following question into a Cypher query using the provided graph schema!\""
      ],
      "metadata": {
        "id": "GU24PLm6ei9S"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# List to collect the samples\n",
        "trainer=[]"
      ],
      "metadata": {
        "id": "7dUqfV6cmRxY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**NOTES:**\n",
        "\n",
        "- With the exception of the first couple of examples, samplers that pertain to a single node label are generated using ```utilities.build_node_sampler(nlist, prompter, allow_repeats)```. Here `nlist` takes the structure `dparsed[\"datatype_parsed\"]` or `dparsed[\"dtypes_parsed\"]`, specifying the data type(s) attributed to the selected properties.\n",
        "\n",
        "- Most of the samples that involve two node labels are constructed via `utilities.build_nodes_property_pairs_sampler(nlist_1, nlist_2, prompter, same_node, allow_repeats)`. Here `nlist_1` and `nlist_2` are as in the previous case and allow for independent choices of data types for the nodes properties. The `same_node` is a boolean argument, that controls how the two nodes are selected.\n",
        "\n",
        "- The samples that involve named relationships are constructed with `utilities.build_relationship_samples(rel_list, prompter, allow_repeats)`. Here `rel_list` is extracted as `drels[\"key\"]` where key indicates the data types of the start node and end node, or it is `all_rel` which takes into account all possible data types available in the graph.\n",
        "\n",
        "- The samples that involve relationship properties are built using  `utilities.build_relationships_props_samples(rel_list, prompter, allow_repeats)` which is similar to the general relationships samples builder from above."
      ],
      "metadata": {
        "id": "4tL4gEp1PKHd"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### One Node Label"
      ],
      "metadata": {
        "id": "4UyECXmTfYCG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def count_nodes_of_given_label():\n",
        "    \"\"\" Determine how many nodes of specified label are in the graph.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "\n",
        "        subschema =  build_minimal_subschema(jschema,[[label_1, ]],[], False, False, False)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the total number of {label_1} in the graph!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) RETURN count(n)\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    sampler = []\n",
        "    for label in nodes:\n",
        "        temp_dict = prompter(label)\n",
        "        sampler.append(temp_dict)\n",
        "\n",
        "    return sampler\n",
        "\n",
        "# Build the set\n",
        "sampler = count_nodes_of_given_label()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "_mxDP2euv0iL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def paths_with_node_endpoint():\n",
        "    \"\"\"Find paths with specified endpoints.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema,[[label_1, ]],[], False, False, False)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Identify three paths where {label_1} is a start or end node!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH p=(b:{label_1})-[r*]->(n) RETURN p UNION MATCH p=(n)-[r*]->(b:{label_1}) RETURN p LIMIT 3\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    sampler = []\n",
        "\n",
        "    for label in nodes:\n",
        "        temp_dict = prompter(label)\n",
        "        sampler.append(temp_dict)\n",
        "\n",
        "    return sampler\n",
        "\n",
        "# Build the set\n",
        "sampler = paths_with_node_endpoint()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "MLvXQd7iv_3C"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### One Node Label, One Property"
      ],
      "metadata": {
        "id": "h6VewQ-y0ruX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Any Data Type Input"
      ],
      "metadata": {
        "id": "GVYXSqlU8KmY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def match_one_node_one_prop():\n",
        "    \"\"\"Return a given node label and a specified property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        # Extract subschema for the variables of interest\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch the {label_1} nodes and extract their {prop_1} property!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) RETURN n.{prop_1}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_one_node_one_prop()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "a--D9OoN_NSO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_prop_notnull_numeral():\n",
        "    \"\"\"Return n (use figures, e.g. 8) nodes where a property is not null.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find 10 {label_1} that have the {prop_1} recorded and return these values!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NOT NULL RETURN n.{prop_1} LIMIT 10\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                 prompter,\n",
        "                                 allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_prop_notnull_numeral()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "jiB6wbs4uA1W"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_prop_notnull_literal():\n",
        "    \"\"\"Return n (use words, e.g. eight) nodes where a property is not null.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find ten {label_1} that have {prop_1} and return their records!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NOT NULL RETURN n.{prop_1} LIMIT 10\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                 prompter,\n",
        "                                 allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_prop_notnull_literal()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Iu39LMP4hHp5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_prop_null_numeral():\n",
        "    \"\"\"Return n (use figures, e.g. 8) nodes where a property is null.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find 8 {label_1} that are missing the {prop_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NULL RETURN n LIMIT 8\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_prop_null_numeral()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "hrRFLxduiEet"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_notproperty_count():\n",
        "    \"\"\"Find how many nodes of given label are missing a specified property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the total number of {label_1} for which the {prop_1} is missing!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NULL RETURN count(n)\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_notproperty_count()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "CtDCvue7vN5z"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_property_count():\n",
        "    \"\"\"Count nodes of given label which have a certain property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the total number of {label_1} that have the {prop_1} recorded!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NOT NULL RETURN count(n)\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_property_count()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "wTHDbzSmvN-w"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_by_property():\n",
        "    \"\"\"Find instances of given node label that has a property with specified value.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        # Extract subschema for the variables of interest\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_1} for which {prop_1} is {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1} {{{prop_1}:'{val_1}'}}) RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler =  find_node_by_property()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "UaEpwspVv0dz"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_skip_limit_return_property():\n",
        "    \"\"\"Return a list of values of a property, using skip and limit.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        nrecs = kwargs.get('nrecs', 2)\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Return the {prop_1} of the {label_1}, skip the first {nrecs} records and return {nrecs} records!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) RETURN n.{prop_1}  SKIP {nrecs} LIMIT {nrecs}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = match_skip_limit_return_property()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Fzubecb9uXYO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### String Data Type"
      ],
      "metadata": {
        "id": "x7Vao7BN6J_s"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def match_where_skip_limit_return_property():\n",
        "    \"\"\"Fetch a list of nodes with certain properties, use skip and limit.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        nrecs = kwargs.get('nrecs', 2)\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_1} for which {prop_1} starts with {val_1[0]}, skip the first {nrecs} records and return the next {nrecs} records of {prop_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} STARTS WITH '{val_1[0]}' WITH n.{prop_1} AS {prop_1} SKIP {nrecs} LIMIT {nrecs} RETURN {prop_1}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"string_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_where_skip_limit_return_property()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "BJj5G06nuXbm"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_prop_one_val():\n",
        "    \"\"\"Retrieve nodes of given label where a string property has a given value.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_1} where {prop_1} is {val_1.strip()}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} = '{val_1}' RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"string_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_prop_one_val()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "CvukJIk_uA9s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_string_contains():\n",
        "    \"\"\"Retrieve nodes of specified label where a string property contains a given substring.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_1} where {prop_1} contains {val_1[:5]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} CONTAINS '{val_1[:5]}' RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"string_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_string_contains()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "78pIG7W9uA5j"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_by_start_substring():\n",
        "    \"\"\"Find instances of given node label that has a property that starts with a specified substring.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_1} for which {prop_1} starts with {val_1[:3]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} STARTS WITH '{val_1[:3]}' RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"string_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_by_start_substring()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "6QPG8ZFzv0ZE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_string_re():\n",
        "    \"\"\"Retrieve nodes of given label with a string property satisfies a condition given by a regular expression.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch the {label_1} where {prop_1} ends with {val_1[:2]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} =~'{val_1[:2]}.*' RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"string_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_string_re()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "wuNAH2rbuXeS"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Temporal Data Types"
      ],
      "metadata": {
        "id": "HO1JOScy5pmY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_count_in_interval():\n",
        "    \"\"\"Node count for a given time interval.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"How many {label_1} have {prop_1} between January 1, 2010 and January 1, 2015?!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} >= date('2010-01-01') AND n.{prop_1} <= date('2015-01-01') RETURN count(n) AS {label_1}s\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "\n",
        "    return build_node_sampler(dparsed[\"date_parsed\"], # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_count_in_interval()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "ZkVCmaK6y7zc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_nodes_today():\n",
        "    \"\"\"Find nodes with property dated within the last 24 hours.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"List {label_1} that have {prop_1} in the last 24 hours!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} > datetime() - duration('P1D') RETURN n\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_nodes_today()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "XF0Bc1ixzmIk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_nodes_monday():\n",
        "    \"\"\"Find the count of nodes with given label and specified property dated on a Monday.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"How many {label_1} have {prop_1} on a Monday?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE date(n.{prop_1}).weekday = 1 RETURN count(n)\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_nodes_monday()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "qhCg5qUNzl2T"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_property_after_hour():\n",
        "    \"\"\"Find the count of nodes with given label and specified property dated after a given date and time.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find how many {label_1}s have {prop_1} after 6PM, January 1, 2020?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} >= datetime('2010-01-01T18:00:00') RETURN count(n) AS {label_1}s\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                 prompter,\n",
        "                                 allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_property_after_hour()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "llUCYaG4y7wC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_prop_equals_year():\n",
        "    \"\"\"Retrieve nodes of given label where a property has a specific year.\"\"\"\n",
        "\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        date_year = kwargs.get('date_year', 2010)\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch {label_1} where {prop_1} is in {date_year}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE date(n.{prop_1}).year = {date_year} RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_prop_equals_year()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "HyA1lRdr-BzD"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_one_prop_equals_date():\n",
        "    \"\"\"Retrieve nodes of given label where a date property has a specified value.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find {label_1} such that {prop_1} is {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} = date('{val_1}') RETURN n\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_one_prop_equals_date()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "UO50Vla_iENi"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Paths and Neighbors - Any Data Type"
      ],
      "metadata": {
        "id": "Z8hJuusy6XbH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_unique_rels():\n",
        "    \"\"\"Fetch unique relationships that have a given node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"How many unique relationships originate from {label_1} where {prop_1} is {val_1}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->() RETURN COUNT(DISTINCT TYPE(r)) AS rels, TYPE(r)\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_unique_rels()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "SdjM7MowvN0T"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def connection_thru_two_rels():\n",
        "    \"\"\"How many nodes are connected to a given node instance via two relationships.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"How many nodes are connected to {label_1} for which {prop_1} is {val_1}, by exactly two different types of relationships?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->(n) WITH n, COLLECT(DISTINCT TYPE(r)) AS Types WHERE SIZE(Types) = 2 RETURN COUNT(n)\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = connection_thru_two_rels()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Jp9-O1iAv_7h"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def rels_and_counts_and_nodes():\n",
        "    \"\"\"Get information on nodes connected to a certain node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"List the nodes that are connected to {label_1} for which {prop_1} is {val_1}, with their relationship types and count these types!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->(n) RETURN n, TYPE(r) AS Relations, COUNT(r) AS Counts\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler =  rels_and_counts_and_nodes()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "ninYwHbhw2XJ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def rels_and_counts():\n",
        "    \"\"\"Find relationships and their counts that are connected to a specified node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"List the types of relationships and their counts connected to {label_1} for which {prop_1} is {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->() RETURN TYPE(r) AS Relations, COUNT(r) AS Counts\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler =  rels_and_counts()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "t5HT023Vw2Q6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_neighbours():\n",
        "    \"\"\"Find all neighbors of a given node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find all nodes directly connected to the {label_1} that has {prop_1} {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH path=(:{label_1} {{{prop_1}:'{val_1}'}})-->() RETURN path\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_neighbours()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "bAgABpCcxwaY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_neighbors_properties():\n",
        "    \"\"\"Find the neighbors of a given node (specified intrinsically) and list their properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the nodes connected to {label_1} where {prop_1} is {val_1} and list their properties!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->(n) RETURN properties(n), r\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_neighbors_properties()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Se042f_pxwRl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_neighbors_properties():\n",
        "    \"\"\"Find the neighbors of a given node (specified extrinsically) and list their properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Identify nodes that are connected to {label_1} where {prop_1} is {val_1} and list their properties, including those of {label_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (b:{label_1})-[r]->(n) WHERE b.{prop_1} = '{val_1}' RETURN properties(b) AS {label_1}_props, properties(n) AS props\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_neighbors_properties()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "i6h-Z5Bhx5EQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_properties_neighbors_relationship():\n",
        "    \"\"\"Find properties of specified neighbors of a given node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"What are the properties of nodes connected to {label_1} for which {prop_1} is {val_1}, and what are their relationships to {label_1}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (c:{label_1})<-[r]-(n) WHERE c.{prop_1} = '{val_1}' RETURN properties(n) AS props, r\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_properties_neighbors_relationship()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "g_yT9ftbyGNf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def nodes_connected_to_two_nodes():\n",
        "    \"\"\"Find common neighbors of two nodes, only one specified.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Which nodes are connected to {label_1} where {prop_1} is {val_1}, and also to another node?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->(n), (n)-[s]->(m) RETURN labels(n) AS Interim, labels(m) AS Target\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = nodes_connected_to_two_nodes()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "KLkRPm8TDtvi"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def longest_path_from_node():\n",
        "    \"\"\"Find the longest path originating from a given node, basic approach.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Identify the longest path originating from {label_1} for which {prop_1} is {val_1}, and list the properties of the nodes on the path!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH p=(a:{label_1}{{{prop_1}:'{val_1}'}})-[*]->(n) RETURN p, nodes(p) ORDER BY LENGTH(p) DESC LIMIT 1\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = longest_path_from_node()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "BOBkF9LryGJ7"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def node_properties_for_two_relationships():\n",
        "    \"\"\"Fetch node properties for a given path.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"What are the properties of nodes connected to {label_1} where {prop_1} is {val_1}, by two different types of relationships?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (e:{label_1}{{{prop_1}:'{val_1}'}})-[r1]->(n)-[r2]->(m) WHERE TYPE(r1) <> TYPE(r2) RETURN properties(n) AS props1, properties(m) AS props2\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = node_properties_for_two_relationships()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "6vK1dPaOyrsa"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def average_props():\n",
        "    \"\"\"Find the average count of properties of nodes along a path.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"What is the average number of properties per node connected to {label_1} for which {prop_1} is {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->(n) RETURN AVG(SIZE(keys(n))) AS AvgProps\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = average_props()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "wyxUedm6yrg2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def first_and_far_neighbors():\n",
        "    \"\"\"Proprieties of nodes for which there is a path to a specified node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Enumerate the properties of nodes that are either directly or indirectly connected to {label_1} for which {prop_1} is {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[*]->(n) RETURN DISTINCT properties(n) AS Properties\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = first_and_far_neighbors()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "D1aAlHxwDA0r"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def nodes_connected_to_node():\n",
        "    \"\"\"Find the neighbors of a node (extrinsincally specified property).\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\" List all nodes that are connected to {label_1} where {prop_1} contains {val_1}, along with the type of their relationship with {label_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (d:{label_1})-[r]->(n) WHERE d.{prop_1} CONTAINS '{val_1}' RETURN n, TYPE(r)\"\"\"\n",
        "                           }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = nodes_connected_to_node()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "8tfBc8ef8f-T"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_far_unique_rels():\n",
        "    \"\"\"Find the distinct properties of nodes that are nhops away from a given node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        nhops = kwargs.get('nhops', 2)\n",
        "\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"List the distinct properties of nodes that are {nhops} hops away from {label_1} with {prop_1} equal to {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[*{nhops}]->(n) RETURN DISTINCT properties(n) AS props\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    sampler = []\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_far_unique_rels()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "hYC34FWPIXbW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_far_neighbors_properties():\n",
        "    \"\"\"Find the properties of nodes that are 3 hops away from a given node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        nhops = kwargs.get('nhops', 3)\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"List the properties of nodes that are {nhops} hops away from {label_1} with {prop_1} equal to {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\" MATCH (a:{label_1})-[*{nhops}]->(n) WHERE a.{prop_1} = '{val_1}' RETURN properties(n) AS props\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_far_neighbors_properties()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "DEgq9wEaxwJ-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_far_neighbors():\n",
        "    \"\"\"Retrieve the node labels of the nodes that are nhops away from a given node instance.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        nhops = kwargs.get('nhops', 3)\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"List nodes that are {nhops} hops away from {label_1} for which {prop_1}={val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[*{nhops}]->(n) RETURN labels(n) AS FarNodes\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_node_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                              prompter,\n",
        "                              allow_repeats= ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_far_neighbors()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "GExqg5fGx5Sk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### One Node Label, Two Properties"
      ],
      "metadata": {
        "id": "ZFVSi_NINKDT"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### String Data Type"
      ],
      "metadata": {
        "id": "bkpE4tSS5qp1"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def match_with_where_not_value():\n",
        "    \"\"\"Retrieve a node property when another property does not take a certain value.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Retrieve distinct values of the {prop_2} from {label_1} where {prop_1} is not {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} <> '{val_1}' RETURN DISTINCT n.{prop_2} AS {prop_2}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = match_with_where_not_value()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "7trkkRB-GUVL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_with_where_contains_substring():\n",
        "    \"\"\"Retrieve two properties of a node if one of the properties does contain a given substring.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {prop_1} and the {prop_2} for those {label_1} where {prop_1} contains the substring {val_1[:2]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} CONTAINS '{val_1[2:]}' RETURN n.{prop_1} AS {prop_1}, n.{prop_2} AS {prop_2}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"string_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_with_where_contains_substring()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "qk0drj5n_68u"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_with_where_starts_with_substring():\n",
        "    \"\"\"Retrieve two properties of a node if one of the properties starts with a given substring.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {prop_1} and the {prop_2} for those {label_1} where {prop_1} starts with {val_1[0]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} STARTS WITH '{val_1[0]}' RETURN n.{prop_1} AS {prop_1}, n.{prop_2} AS {prop_2}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"string_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_with_where_starts_with_substring()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "FKNp26qPC0Yt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_with_where_not_is_value():\n",
        "    \"\"\"Return two properties of a node if one of the properties does not start with a specified string.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch unique values of {prop_1} and {prop_2} from {label_1} where {prop_1} does not start with {val_1[0]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE NOT n.{prop_1} STARTS WITH '{val_1[0]}' RETURN DISTINCT n.{prop_1} AS {prop_1}, n.{prop_2} AS {prop_2}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"string_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_with_where_not_is_value()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "IamjsvkeC0Nl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_properties_with_union():\n",
        "    \"\"\"Find node instances if one of two properties contains a certain substring.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Retrieve the {label_1} where {prop_1} or {prop_2} contains {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} CONTAINS '{val_1}' RETURN n AS node UNION ALL MATCH (m:{label_1}) WHERE m.{prop_2} CONTAINS '{val_1}' RETURN m AS node\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"string_parsed\"],\n",
        "                                              dparsed[\"string_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = True,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_properties_with_union()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "vS0p9KcJ3DmT"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_two_props_notnull_or():\n",
        "    \"\"\"Find a specified property of a given label if another property fulfills a given condition or the specified property is not null.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch the distinct values of the {prop_2} from {label_1} where either {prop_1} is {val_1} or {prop_2} is not null!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} = '{val_1}' OR n.{prop_2} IS NOT NULL RETURN DISTINCT n.{prop_2} AS {prop_2}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"string_parsed\"],\n",
        "                                              dparsed[\"string_parsed\"],\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_two_props_notnull_or()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "uCvKVKse-K8x"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Temporal Data Types"
      ],
      "metadata": {
        "id": "cK14f4eC5xkw"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_property_in_year():\n",
        "    \"\"\"Find a property of a given node if a temporal condition on a second property holds.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find all {prop_1} for {label_1} that have {prop_2} in 2020!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE date(n.{prop_2}).year = 2020 RETURN n.{prop_1}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_property_in_year()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "vBORF9fei6Gg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_property_in_month():\n",
        "    \"\"\"Find how many nodes of have a first property and a temporal condition on a second property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find how many {label_1} with {prop_1} recorded have {prop_2} in June!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NOT NULL AND date(n.{prop_2}).month = 6 RETURN count(n)\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"date_parsed\"],  # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = find_property_in_month()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "JUoGMW71i5_G"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_one_node_two_props_two_vals_or_notnull_date():\n",
        "    \"\"\"Find a temporal property for a specified node label when a second property takes a given value.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "        val_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {prop_2} for those {label_1}s where {prop_1} is {val_1} and the year of the {prop_2} is {val_2[:4]}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} = '{val_1}' AND date(n.{prop_2}).year = {val_2[:4]} RETURN n.{prop_2} AS {prop_2}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"date_parsed\"], # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_one_node_two_props_two_vals_or_notnull_date()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "0kHZ8i0SOoqu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_property_after_date():\n",
        "    \"\"\"Find a temporal property of a given node if a second temporal property satisfies a certain condition.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find all {prop_1} for {label_1} that have {prop_2} after January 1, 2020!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE date(n.{prop_2}) > date('2020-01-01') RETURN n.{prop_1}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"date_parsed\"], # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                              dparsed[\"date_parsed\"], # dparsed[\"date_parsed\"]+dparsed[\"date_time_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_property_after_date()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "9fIzPoG5i4l2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Numerical Data Types"
      ],
      "metadata": {
        "id": "o2fGDMS753Ld"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Node count by property and relation\n",
        "def aggregate_integers_by_string():\n",
        "    \"\"\"Find statistics of a numerical property for those nodes that satisfy a condition on a second property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"For each nonull {prop_1} of the {label_1}, how many times does it appear, and what are the minimum, maximum and average values of {prop_2} associated to it?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} IS NOT NULL WITH DISTINCT n WITH n.{prop_1} as {prop_1}, COUNT(n) AS count, min(n.{prop_2}) AS min, max(n.{prop_2}) AS max, avg(n.{prop_2}) AS avg RETURN {prop_1}, count, min, max, avg\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"integer_parsed\"], # dparsed[\"integer_parsed\"]+dparsed[\"float_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = aggregate_integers_by_string()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "HqYNEeNnh5WT"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_with_where_not_null():\n",
        "    \"\"\"Return nodes where a property is not null, a second property takes specified values, order by the second property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        prop_2 = params[3]\n",
        "        val_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Search for {prop_1} and {prop_2} from {label_1} where {prop_1} is not null and {prop_2} exceeds {val_2} and sort the results by {prop_2}, beginning with the largest!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1}  IS NOT NULL AND n.{prop_2} > {val_2} RETURN n.{prop_1} AS {prop_1}, n.{prop_2} AS {prop_2} ORDER BY {prop_2} DESC\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"string_parsed\"],\n",
        "                                              dparsed[\"integer_parsed\"], # dparsed[\"integer_parsed\"]+dparsed[\"float_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_with_where_not_null()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Y6BlwGTAH3Wl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def aggregate_numerical_by_integer():\n",
        "    \"\"\"Count the nodes where two properties satisfy two numerical conditions.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        prop_2 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_1} counts where {prop_1} is smaller than ten, and return the maximum, minimum and average values of the {prop_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} > 100 WITH DISTINCT n WITH n.{prop_1} as {prop_1}, COUNT(n) AS count, min(n.{prop_2}) AS min_{prop_2}, max(n.{prop_2}) AS max_{prop_2}, avg(n.{prop_2}) AS avg_{prop_2} RETURN {prop_1}, count, min_{prop_2}, max_{prop_2}, avg_{prop_2}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"integer_parsed\"], # dparsed[\"integer_parsed\"]+dparsed[\"float_parsed\"] when available\n",
        "                                              dparsed[\"integer_parsed\"], # dparsed[\"integer_parsed\"]+dparsed[\"float_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = aggregate_numerical_by_integer()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "qk2YtagWh5MC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_with_where_or_numerical_literal():\n",
        "    \"\"\"Find at most n nodes of specified label where a numerical property is greater or another is less than specified values.\"\"\"\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        prop_2 = params[3]\n",
        "        val_2 = params[4]\n",
        "\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_1, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find eight instances of {label_1} where either {prop_1} exceeds {val_1} or {prop_2} is less than {val_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE n.{prop_1} > {val_1} OR n.{prop_2} < {val_2} RETURN n LIMIT 8\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"integer_parsed\"], # dparsed[\"integer_parsed\"]+dparsed[\"float_parsed\"] when available\n",
        "                                              dparsed[\"integer_parsed\"], # dparsed[\"integer_parsed\"]+dparsed[\"float_parsed\"] when available\n",
        "                                              prompter,\n",
        "                                              same_node=True,\n",
        "                                              allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_with_where_or_numerical_literal()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "SJtKmkwLCWax"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Two Node Labels, Properties\n",
        "\n",
        "---\n",
        "\n"
      ],
      "metadata": {
        "id": "EufGIDcliOIs"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Relationships to Nodes"
      ],
      "metadata": {
        "id": "pxIolMlb48AS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_nodes_connected_to_two_nodes():\n",
        "    \"\"\"Find the nodes connected to two given nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        label_2 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ], [label_2, ]], [], False, False, False)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find nodes that share a relationship with both {label_1} and {label_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (c:{label_1})<-[r1]-(n)-[r2]->(d:{label_2}) RETURN labels(n)\"\"\"\n",
        "                           }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_pairs(nodes,\n",
        "                             prompter,\n",
        "                             allow_repeats = ALLOW_REPEATS\n",
        "                             )\n",
        "\n",
        "# Build the set\n",
        "sampler= find_nodes_connected_to_two_nodes()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Zh_TRQ4V5cip"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def nodes_connected_to_two_nodes_both():\n",
        "    \"\"\"Find nodes on paths between two given nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        label_2 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ], [label_2, ]], [], False, False, False)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Identify nodes that are connected to both {label_1} and {label_2}, directly or indirectly!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (a:{label_1})-[*]-(n)-[*]-(b:{label_2}) RETURN labels(n)\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_pairs(nodes,\n",
        "                             prompter,\n",
        "                             allow_repeats = ALLOW_REPEATS\n",
        "                             )\n",
        "\n",
        "# Build the set\n",
        "sampler = nodes_connected_to_two_nodes_both()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "EWcByGHh7pRD"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_common_rels():\n",
        "    \"\"\"Find nodes that share common relationships with two given nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        label_2 = params[1]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ], [label_2, ]], [], False, False, False)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Are there any nodes that share a common relationship type with both {label_1} and {label_2}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (a:{label_1})-[r]->(n), (d:{label_2})-[s]->(m) WHERE TYPE(r) = TYPE(s) RETURN labels(n), labels(m)\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_pairs(nodes,\n",
        "                             prompter,\n",
        "                             allow_repeats = ALLOW_REPEATS\n",
        "                             )\n",
        "\n",
        "# Build the set\n",
        "sampler = find_common_rels()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "U4gghCY0EYpC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def rel_and_common_prop():\n",
        "    \"\"\"Identify nodes with common properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "        val_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Are there any nodes that are connected with {label_1} where {prop_1} is {val_1} and share a common property with {label_2}, for which {prop_2} equals {val_2}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r]->(n), (d:{label_2}{{{prop_2}:'{val_2}'}}) WHERE ANY(key in keys(n) WHERE n[key] = d[key]) RETURN n\"\"\"}\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "\n",
        "# Build the set\n",
        "sampler = rel_and_common_prop()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "ZfOGINdXDPX_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Unions of Sets"
      ],
      "metadata": {
        "id": "4scZ11yE5G1z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def match_nodes_with_union_all():\n",
        "    \"\"\"Build a union of two sets (without filtering duplicates) extracted from two distinct node labels and their properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Return the {prop_1} for {label_1} combined with the {prop_2} for {label_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) RETURN n.{prop_1} AS Records UNION ALL MATCH (m:{label_2}) RETURN m.{prop_2} AS Records\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_nodes_with_union_all()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "CNAPHUWWLxUL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def match_nodes_with_union():\n",
        "    \"\"\"Build a union of two sets (with filtering duplicates) extracted from two distinct node labels and their properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Return the {prop_1} for {label_1} combined with the {prop_2} for {label_2}, filter the duplicates if any!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) RETURN n.{prop_1} AS Records UNION MATCH (m:{label_2}) RETURN m.{prop_2} AS Records\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = match_nodes_with_union()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "JuAkIDPRJBr4"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Retrieve Properties"
      ],
      "metadata": {
        "id": "owroLR-D5Ntl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def match_two_nodes_two_props():\n",
        "    \"\"\"Retrieve several samples of properties values that correspond to two node labels (same or distinct).\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch eight samples of the {prop_1} of the {label_1} and the {prop_2} for {label_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) MATCH (m:{label_2}) RETURN n.{prop_1}, m.{prop_2} LIMIT 8\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "sampler = match_two_nodes_two_props()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "IM1fVQXFx_tu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_not_simple_path_and_property():\n",
        "    \"\"\"Retrieve one property that is not in relationship to another node with a given property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "        val_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Look for the {prop_1} of the {label_1} that is not related  to the {label_2} with the  {prop_2}  {val_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}), (:{label_2} {{{prop_2}: '{val_2}'}}) WHERE NOT (n) --> (:{label_2}) RETURN n.{prop_1}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_not_simple_path_and_property()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "WpXGNCrpy07F"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Paths"
      ],
      "metadata": {
        "id": "lbsj6qwG5W2B"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def path_existence():\n",
        "    \"\"\"Determine if there is a path connected two given nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "        val_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Is there a path connecting {label_1} where {prop_1} is {val_1} and {label_2}, for which {prop_2} is {val_2}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}}), (b:{label_2}{{{prop_2}:'{val_2}'}}) RETURN EXISTS((a)-[*]-(b)) AS pathExists\"\"\"}\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = path_existence()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "zLsNhFfUqJSK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def number_of_paths():\n",
        "    \"\"\"Find the number of paths with given end nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "        val_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"How many paths are there between {label_1} where {prop_1} is {val_1} and {label_2}, for which {prop_2} equals {val_2}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH p=(a:{label_1}{{{prop_1}:'{val_1}'}})-[*]->(d:{label_2}{{{prop_2}:'{val_2}'}}) RETURN count(p)\"\"\"}\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = number_of_paths()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "ScTLEqr2DGbP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def end_of_the_path():\n",
        "    \"\"\"Find the end node of a given path.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "        val_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find nodes that are at the end of a path starting at {label_1} where {prop_1} is {val_1} and traversing through {label_2} with {prop_2} {val_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[*]->(d:{label_2}{{{prop_2}:'{val_2}'}})-[*]->(n) RETURN n\n",
        "                    \"\"\" }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = end_of_the_path()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "yOz9LaDC9VHY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def shortest_path_between_two_nodes():\n",
        "    \"\"\"Find the shortest path between two nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        label_2 = params[3]\n",
        "        prop_2 = params[4]\n",
        "        val_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2]], [], True, False, True)[:-29] # remove relationship comment\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the shortest path between {label_1} where {prop_1} is {val_1} and {label_2}, with {prop_2} equal {val_2}, including the nodes on the path!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH p=shortestPath((a:{label_1}{{{prop_1}:'{val_1}'}})-[*]-(e:{label_2}{{{prop_2}:'{val_2}'}})) RETURN nodes(p)\n",
        "                    \"\"\"\n",
        "                           }\n",
        "        return message\n",
        "\n",
        "    return build_nodes_property_pairs_sampler(dparsed[\"dtypes_parsed\"],\n",
        "                                              dparsed[\"dtypes_parsed\"],\n",
        "                                       prompter,\n",
        "                                       same_node = False,\n",
        "                                       allow_repeats = ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler =  shortest_path_between_two_nodes()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "YcZ35Pat80s-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Relationships\n",
        "\n",
        "Use only one set of instances for no repeats.\n",
        "build_minimal_subschema(jschema, [[label_1, ], [label_2, ]], [], False, False, False)[:-29] # remove relationship comment"
      ],
      "metadata": {
        "id": "-iIMbBYQiyjX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Nodes and Relationships"
      ],
      "metadata": {
        "id": "lGjtwU8lv9gQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_not_connected_nodes():\n",
        "    \"\"\"Identify nodes that do not have certain relationships.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        rel_1 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ]], [[rel_1, ]], False, False, False)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch five {label_1} that are not linked through {rel_1} relationships!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (p:{label_1}) WHERE NOT EXISTS ((p)-[:{rel_1}]->()) RETURN p LIMIT 5\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_not_connected_nodes()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "YpoQ5IURg7pV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_connected_nodes():\n",
        "    \"\"\"Find nodes that are connected via certain relationships.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        rel_1 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ]], [[rel_1, ]], False, False, False)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find four {label_1} that have {rel_1} links!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (p:{label_1}) WHERE EXISTS ((p)-[:{rel_1}]->()) RETURN p LIMIT 4\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_connected_nodes()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "3EirfO-Rg7f8"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_count():\n",
        "    \"\"\"Count the number of specified relationships a node has.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1]], [[rel_1, ]], True, False, False)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch ten {label_1} and return the {prop_1} and the number of nodes connected to them via {rel_1} given in descending order of the node counts.\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WITH n.{prop_1} AS {prop_1}, size([(n)-[:{rel_1}]->() | 1]) AS count ORDER BY count DESC LIMIT 10 RETURN article_id, count\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_count()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "uSxJX2Trgitn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Two Labels, One Property"
      ],
      "metadata": {
        "id": "LZ_mMsxQ6li-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def nodes_connected_to_first_node_and_not_connected_to_second_node():\n",
        "    \"\"\"Determine which nodes are connected to node A but not connected to node B via a given relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1,], [label_2, ]], [[rel_1, ]], False, False, False)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\" Which nodes are connected to {label_1}, but not to {label_2} via {rel_1}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (c:{label_1})-[r]-(n) WHERE NOT (n)-[:{rel_1}]-(:{label_2}) RETURN labels(n)\"\"\"\n",
        "                           }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler =  nodes_connected_to_first_node_and_not_connected_to_second_node()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "UhW2f10c8LrY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_property_with_count_limit():\n",
        "    \"\"\"Retrieve property values for several nodes A and the number of relationship counts to nodes B.\"\"\"\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Search for the {prop_1} values from 20 {label_1} that are linked to {label_2} via {rel_1} and return {prop_1} along with the respective {label_2} counts!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WITH DISTINCT n, m RETURN n.{prop_1} AS {prop_1}, count(m) AS count LIMIT 20\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_property_with_count_limit()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "xlt4x4ANiaGV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_property_by_condition_on_node():\n",
        "    \"\"\"Retrieve property values for nodes A that have more than five relationships to nodes B.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {prop_1} of {label_1} that each have more than five {rel_1} relationships with {label_2}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[r:{rel_1}]->(m:{label_2}) WITH DISTINCT n, m, r WITH n.{prop_1} AS {prop_1}, count(r) AS count WHERE count > 5 RETURN {prop_1}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_property_by_condition_on_node()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "iH43Uq6XiYGN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_and_exists_simple_path():\n",
        "    \"\"\"Fetch a property of nodes connected to a given node via a specified relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch {prop_1} of the {label_1} that are connected to {label_2} via {rel_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE EXISTS {{ MATCH (n)-[:{rel_1}]->(:{label_2}) }} RETURN n.{prop_1} AS {prop_1}\"}\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"string_string_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_and_exists_simple_path()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "ghekBU5Py1Nc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_ordered_count_desc():\n",
        "    \"\"\"Retrieve, in descending order, the count of nodes linked to a given node.\"\"\"\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"For each {label_1} find its {prop_1} and the count of {label_2} linked via {rel_1}, and retrieve seven results in desc order of the counts!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WITH DISTINCT n, m RETURN n.{prop_1} AS {prop_1}, count(m) AS count ORDER BY count DESC LIMIT 7\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_ordered_count_desc()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "CcD0A3dxg7I4"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_ordered_count():\n",
        "    \"\"\"Retrieve, in ascending order, the counts of nodes linked to a given node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"For each {label_1}, find the number of {label_2} linked via {rel_1} and retrieve the {prop_1} of the {label_1} and the {label_2} counts in ascending order!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WITH DISTINCT n, m RETURN n.{prop_1} AS {prop_1}, count(m) AS {label_2.lower()}_count ORDER BY {label_2.lower()}_count\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_ordered_count()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "qomhpn-pg7Um"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_ordered_count_filter():\n",
        "    \"\"\"Retrieve the counts, larger than a given value, of nodes linked to a given node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"For each {label_1} and its {prop_1}, count the {label_2} connected through {rel_1} and fetch the {prop_1} and the counts that are greater than 5, starting with the largest {prop_1} and count!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WITH DISTINCT n, m WITH n.{prop_1} AS {prop_1}, count(m) AS count WHERE count > 4 RETURN {prop_1}, count ORDER BY {prop_1} DESC, count DESC\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_ordered_count_filter()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "zNNk4NLJg66c"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_common_prop():\n",
        "    \"\"\"Find related nodes with common properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Which nodes have a common property with {label_1} where {prop_1} is {val_1} and are {rel_1} linked to a {label_2}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (a:{label_1} {{{prop_1}:'{val_1}'}})-[r:{rel_1}]->(b:{label_2}) WHERE ANY(key IN keys(a) WHERE a[key] = b[key]) RETURN b\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_common_prop()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "CCQyRC-YESqC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_end_nodes_path():\n",
        "    \"\"\"Find nodes that are at the end of a path with specified starting node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Which nodes are at the end of a path starting from {label_1}, with {prop_1} equal to  {val_1}, passing through {label_2} via {rel_1}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[:{rel_1}]->(c:{label_2})-[r]->(n) RETURN n\"\"\"\n",
        "                           }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_end_nodes_path()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "ky8Wlxm42PAg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Extract properties of end nodes of a path\n",
        "def find_end_node_properties():\n",
        "    \"\"\"Find properties of nodes connected to specified nodes.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"What are the properties of {label_2} that is {rel_1} connected to {label_1} that has {prop_1} equal to {val_1}?\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WHERE n.{prop_1} = {val_1} RETURN properties(m) AS props\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_end_node_properties()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "FkgQ6jnt1dFE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Two Labels, Two Properties"
      ],
      "metadata": {
        "id": "OEQcapG-9hwD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_ordered_count_collect():\n",
        "    \"\"\"Find properties of nodes that are related under given conditions.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwargs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "        prop_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2 ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch the {prop_1} of the {label_1} that are linked via {rel_1} to more than three {label_2}, and list {label_2} {prop_2} and {label_2} counts, ordering by {label_2} count and limiting to the top six results!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WITH DISTINCT n, m WITH n.{prop_1} AS {prop_1}, count(m) AS count, COLLECT(m.{prop_2}) as {prop_2} WHERE count > 3 RETURN {prop_1}, count, {prop_2} ORDER BY count LIMIT 6\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "# Build the set\n",
        "sampler = find_node_relation_ordered_count_collect()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Hg6AX9oWh3dU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Node count by property and relation\n",
        "def find_node_aggregation_date_rels():\n",
        "    \"\"\"Evaluate the average values of a property for all nodes of the same label that are connected to a specified node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "        prop_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2 ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Calculate the average {prop_2} for {label_2} that are linked to {label_1} via {rel_1} and have {prop_1} date before December 31, 2020!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}]->(m:{label_2}) WHERE m.{prop_1} < date('2020-12-31') RETURN avg(m.{prop_2}) AS avg_{prop_2}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],  # best with drels[\"date_integer_rels\"] or drels[\"date_float_rels\"] if available\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_aggregation_date_rels()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "l0DvIvr6i4_c"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_and_simple_path():\n",
        "    \"\"\"Find a property of a node connected via a given relationship to a node for which a certain property takes a specified value.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "        prop_2 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2 ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": \"Convert the following question into a Cypher query using the provided graph schema!\",\n",
        "                   \"Question\": f\"\"\"Retrieve the {prop_2} for {label_2} that is linked through a {rel_1} relationship with the {label_1} where {prop_1} is {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[{rel_1[:2].lower()}:{rel_1}]->(m) WHERE n.{prop_1}='{val_1}' RETURN m.{prop_2}\"\n",
        "                   }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_and_simple_path()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "syzz0J40UgxK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def relation_with_and_where():\n",
        "    \"\"\"Retrieve related node properties that satisfy given conditions.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        label_2 = params[4]\n",
        "        prop_2 = params[5]\n",
        "        val_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2, prop_2 ]], [[rel_1, ]], True, False, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find {label_2} that has a {prop_2} which begins with {label_2[0].lower()}, and is linked to {label_1} via {rel_1} relationship, where {label_1} has {prop_1} {val_1}!\"\"\",\n",
        "                   \"Schema\": f\"Graph schema: {subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1} {{{prop_1}: '{val_1}'}}) -[:{rel_1}]- (m:{label_2}) WHERE m.{prop_2} STARTS WITH '{label_2[0].lower()}' RETURN m\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_samples(drels[\"string_string_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = relation_with_and_where()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "0RUazq188N0Y"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Relationships with Properties"
      ],
      "metadata": {
        "id": "miuuWVqUK1rA"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Nodes and Relationships (with properties)"
      ],
      "metadata": {
        "id": "HVcsBEZSK7tN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Pattern check\n",
        "def find_not_connected_nodes_relprops():\n",
        "    \"\"\"Identify nodes that do not have certain relationships.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1= params[0]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ]], [[rel_1,rprop_1]],False, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch five {label_1} that are not linked through {rel_1} relationships where {rprop_1} is {rval_1}!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (p:{label_1}) WHERE NOT EXISTS {{(p)-[r:{rel_1}]->() WHERE r.{rprop_1}='{rval_1}' }} RETURN p LIMIT 5\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_not_connected_nodes_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "d5PuGK9TKDty"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_connected_nodes_relprops():\n",
        "    \"\"\"Find nodes that are connected via certain relationships.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, ]], [[rel_1,rprop_1 ]],False, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find four {label_1} that have {rel_1} links so that {rprop_1} are {rval_1}!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (p:{label_1}) WHERE EXISTS {{(p)-[r:{rel_1}]->() WHERE r.{rprop_1}='{rval_1}'}}  RETURN p LIMIT 4\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_connected_nodes_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "hz-oPynxKDl_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_count_relprops():\n",
        "    \"\"\"Count the number of specified relationships a node has.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1,prop_1 ]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch all the {label_1} and return the {prop_1} and the number of nodes connected to them via {rel_1} with {rprop_1} = {rval_1}.\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1})-[r:{rel_1}]->() WHERE r.{rprop_1} = '{rval_1}' WITH (n), COUNT(*) AS numberOfDirectConnections RETURN n.{prop_1} AS {prop_1}, numberOfDirectConnections\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_count_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "Bg78V5dFLJmV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Two Labels, One Property, Relationship (with property)"
      ],
      "metadata": {
        "id": "ra0JQHncLNtC"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_property_with_count_limit_relprops():\n",
        "    \"\"\"Retrieve property values for several nodes A and the number of relationship counts to nodes B.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\":  f\"\"\"Search for the {prop_1} values from 20 {label_1} that are linked to {label_2} via {rel_1} with {rprop_1} = {rval_1}, and return {prop_1} along with the respective {label_2} counts!\"\"\",\n",
        "                   \"Schema\":f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[r:{rel_1}]->(m:{label_2}) WHERE r.{rprop_1}='{rval_1}' WITH DISTINCT n, m RETURN n.{prop_1} AS {prop_1}, count(m) AS count LIMIT 20\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_property_with_count_limit_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "kd5reorhLQfX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_and_exists_simple_path_relprops():\n",
        "    \"\"\"Fetch a property of nodes connected to a given node via a relationship with specified properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Fetch {prop_1} of the {label_1} that are connected to {label_2} via {rel_1} where {rprop_1} are at most {rval_1}!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) WHERE EXISTS {{ MATCH (n)-[r:{rel_1}]->(:{label_2}) WHERE r.{rprop_1} < '{rval_1}'}} RETURN n.{prop_1} AS {prop_1}\"}\n",
        "\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"], # use drelsprops[\"string_integer_string_rels\"] if available\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_and_exists_simple_path_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "FTwmu-taLQmd"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_ordered_count_desc_relprops():\n",
        "    \"\"\"Retrieve, in descending order, the count of nodes linked to a given node, via a specified relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"For each {label_1} find its {prop_1} and the count of {label_2} linked via {rel_1} where {rprop_1} is not '{rval_1}', and retrieve seven results in desc order of the counts!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[r:{rel_1}]->(m:{label_2}) WHERE r.{rprop_1} <> '{rval_1}' WITH DISTINCT n, m RETURN n.{prop_1} AS {prop_1}, count(m) AS count ORDER BY count DESC LIMIT 7\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_ordered_count_desc_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "_qJ2DZcfLQjA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_ordered_count_relprops():\n",
        "    \"\"\"Retrieve, a property and the counts, in ascending order, of nodes linked to a given node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"For each {label_1}, find the number of {label_2} linked via {rel_1} where {rprop_1} is {rval_1} and retrieve the {prop_1} of the {label_1} and the {label_2} counts in ascending order!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                    \"Cypher\": f\"MATCH (n:{label_1}) -[r:{rel_1}]->(m:{label_2}) WHERE r.{rprop_1} = '{rval_1}' WITH DISTINCT n, m RETURN n.{prop_1} AS {prop_1}, count(m) AS count ORDER BY count\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_ordered_count_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "bO65zSc5Nkrh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_common_prop_relprops():\n",
        "    \"\"\"Find related nodes with common properties related via a specified relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Which nodes have a common property with {label_1} where {prop_1} is {val_1} and are {rel_1} linked to {label_2}, where {rprop_1} is {rval_1}?\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (a:{label_1}{{{prop_1}:'{val_1}'}})-[r:{rel_1} {{{rprop_1} :'{rval_1}'}}]->(b:{label_2}) WHERE ANY(key IN keys(a) WHERE a[key] = b[key]) RETURN b\"\n",
        "                   }\n",
        "\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_common_prop_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "Jkf--79rNkkM"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "def find_end_nodes_path_relprops():\n",
        "    \"\"\"Find nodes that are at the end of a path with specified starting node and interim relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Which nodes are at the end of a path starting from {label_1}, where {prop_1} is {val_1}, through {label_2} via {rel_1} with {rprop_1} {rval_1}?\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"\"\"MATCH (a:{label_1} {{{prop_1}:'{val_1}'}})-[:{rel_1} {{{rprop_1}: '{rval_1}'}}]->(c:{label_2})-[r]->(n) RETURN n\"\"\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_end_nodes_path_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "WClh69pqN28I"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "def find_end_node_properties_relprops():\n",
        "    \"\"\"Find properties of nodes connected to specified nodes, via specified relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"What are the properties of {label_2} that is {rel_1}, with {rprop_1} equal to {rval_1}, connected to {label_1} that has {prop_1} equal to {val_1}?\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}{{{rprop_1}: '{rval_1}'}}]->(m:{label_2}) WHERE n.{prop_1} = '{val_1}' RETURN properties(m) AS props\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_end_node_properties_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "BBDu5fWyN228"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Two Labels, Two Properties, Relationship (with property)"
      ],
      "metadata": {
        "id": "nGCp2zJROZm0"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_relation_node_count_relprops():\n",
        "    \"\"\"Retrieve properties and counts of nodes connected via a specified relationship.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find {prop_1} of the {label_1} and return it along with the count of {label_2} that are linked via {rel_1} where {rprop_1} is {rval_1}!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[r:{rel_1}]->(m:{label_2}) WHERE r.{rprop_1} = '{rval_1}' RETURN n.{prop_1} AS {prop_1}, count(m) AS count\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = find_node_relation_node_count_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "C7T1HM1UOM4s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def relation_with_and_where_relprops():\n",
        "    \"\"\"Find node properties that are connected via a relationship with non-null property.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        label_2 = params[6]\n",
        "        prop_2 = params[7]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,prop_2]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Find the {label_2} with a {prop_2} starting with {label_2[0]}, and linked with an {label_1} through {rel_1} relationship. The {label_1} must have {prop_1}: {val_1} and be {rel_1} with {rprop_1} recorded!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1} {{{prop_1}: '{val_1}'}}) -[r:{rel_1}]- (m:{label_2}) WHERE m.{prop_2} STARTS WITH '{label_2[0]}' AND r.{rprop_1} IS NOT NULL RETURN n.{prop_2}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = relation_with_and_where_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]\n"
      ],
      "metadata": {
        "id": "xpXb8WsVPLsc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def find_node_aggregation_date_rels_relprops():\n",
        "    \"\"\"Find property average of a node in a specified relationship with another given node.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "        prop_2 = params[7]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,prop_2]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\": f\"\"\"Calculate the average {prop_2} for {label_2} that is linked to {label_1} via {rel_1} where {rprop_1} is {rval_1} and has {prop_1} date before December 31, 2020!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\": f\"MATCH (n:{label_1}) -[:{rel_1}{{{rprop_1}: '{rval_1}'}}]->(m:{label_2}) WHERE m.{prop_1} < date('2020-12-31') RETURN avg(m.{prop_2}) AS avg_{prop_2}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"],  # best with drelsprops[\"date_string_integer_rels\"] when available\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler =find_node_aggregation_date_rels_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "KWZNYvb4O5JP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def where_and_simple_path_relprops():\n",
        "    \"\"\"Retrieve properties of specific nodes that have relationships with given properties.\"\"\"\n",
        "\n",
        "    def prompter(*params, **kwardgs):\n",
        "\n",
        "        label_1 = params[0]\n",
        "        prop_1 = params[1]\n",
        "        val_1 = params[2]\n",
        "        rel_1 = params[3]\n",
        "        rprop_1 = params[4]\n",
        "        rval_1 = params[5]\n",
        "        label_2 = params[6]\n",
        "        prop_2 = params[7]\n",
        "\n",
        "        subschema = build_minimal_subschema(jschema, [[label_1, prop_1], [label_2,prop_2]], [[rel_1,rprop_1 ]],True, True, True)\n",
        "        message = {\"Prompt\": f\"{system_message}\",\n",
        "                   \"Question\":  f\"\"\"Search for the {prop_2} in {label_2} that is linked through a {rel_1} relationship with {label_1} where {prop_1} is {val_1} and {rel_1} has {rprop_1} on {rval_1}!\"\"\",\n",
        "                   \"Schema\": f\"{subschema}\",\n",
        "                   \"Cypher\":  f\"MATCH (n:{label_1}) -[{rel_1[:2].lower()}:{rel_1} {{{rprop_1} : '{rval_1}'}}]->(m) WHERE n.{prop_1}='{val_1}' RETURN m.{prop_2}\"\n",
        "        }\n",
        "        return message\n",
        "\n",
        "    return build_relationships_props_samples(drelsprops[\"all_rels\"], #\"string_date_string_rels\"],\n",
        "                            prompter,\n",
        "                            allow_repeats=ALLOW_REPEATS)\n",
        "\n",
        "# Build the set\n",
        "sampler = where_and_simple_path_relprops()\n",
        "# Print information about the sampler set\n",
        "print(f\"There are {len(sampler)} queries in this subset.\")\n",
        "# Add to trainer dataset\n",
        "trainer += collect_samples(sampler, M)\n",
        "# Display an example for inspection\n",
        "sampler[0]"
      ],
      "metadata": {
        "id": "J2GlL8ApONNH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Data Saving"
      ],
      "metadata": {
        "id": "rJR_6-_a0xK-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import json\n",
        "\n",
        "# Display the number of samples created and save the data to a file\n",
        "print(f\"There are {len(trainer)} samples in the fine-tuning dataset.\")\n",
        "write_json(trainer, data_path+trainer_with_repeats_file)"
      ],
      "metadata": {
        "id": "o-24TBXJMFVp"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}